<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- 
Project Name: flaming-crud
Author: Stephen Cozart
Alias: clip
Email: Stephen.Cozart AT wired4tech DOT com
Web: http://www.wired4tech.com
Source: http://github.com/clip/flaming-crud
File Name: ci_crud.php
File Version: 0.1.3
License: Do whatever you like with this,  however this program comes without warranty.  If you use just drop me a line telling me how much you like it!
-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>flaming-crud</title>
<style>
body {
	padding: 0;
	margin: 0;
	background-color: #E1E1E1;
}
label {
	width: 125px;
	display: block;
	font-style: italic;
	float: left;
}
form, #content, h2 {
	width: 350px;
	margin: 0 auto;
}
form {
	border-top: 1px solid #BFBFBF;
	border-left: 1px solid #BFBFBF;
	border-right: 1px solid #BFBFBF;
	margin-top: 45px;
	width: 350px;
}
form p, form h5 {
	margin: 0;
	padding: 10px;
	border-bottom: 1px solid #BFBFBF;
}
#header {
	width: 100%;
	background-color: #000000;
	color: #FFFFFF;
	height: 60px;
}
#content {
	min-height: 500px !important;
}
h2 {
	line-height: 60px;
	vertical-align: middle;
}
#footer {
	height: 30px;
	width: 100%;
	background-color: #333333;
	color: #FFFFFF;
}
#footer p {
	line-height: 30px;
	vertical-align: middle;
	display: block;
	width: 350px;
	margin: 0 auto;
	font-style: italic;
}
</style>
</head>

<body>
<div id="header">
	<h2>Codeigniter CRUD Generator</h2>
</div>
<div id="content">
<?php if(isset($_POST['submit'])): ?>

<?php 
	$host = $_POST['hostname']; 
	$user = $_POST['username'];
	$pass = $_POST['password'];
	$db = $_POST['database'];
	$table = $_POST['tablename'];
	$modelname = $_POST['modelname'];
	
	//create the database directory if needed.  This will keep the created models organized.
	if(!is_dir($db.'/'))
	{
		mkdir($db.'/');
	}
	
	//create the models directory if needed.
	if(!is_dir($db.'/models'))
	{
		mkdir($db.'/models');
	}
	//connect to the server
	$conn = mysql_connect($host, $user, $pass) or die('Could not connect to host.');
	mysql_select_db($db, $conn) or die('Could not select database.');
	//get the field names
	//$fields = mysql_list_fields($db, $table, $conn);
	//$columns = mysql_num_fields($fields);
	$result = mysql_query("SHOW COLUMNS FROM ". $table, $conn);
	
	//loop through field names and add them to a useable array
	while ($row = mysql_fetch_assoc($result))
	{
		$field_names[] = $row['Field'];
		if($row['Key'] == 'PRI')
		{
			$primary_key = $row['Field'];
		}
	}

	//begin building output
	$output = "<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');" . "\r\n\r\n";
	$output .= "class ".ucfirst($modelname)." extends Model {" . "\r\n\r\n";
	
	//loop through the field names and add them to the output as properties
	foreach($field_names as $property)
	{
		$output .= "\t" . "public $".$property.";" . "\r\n";
	}
	//add static properties
	$output .= "\r\n\t" . "public \$offset;" . "\r\n";
	$output .= "\t" . "public \$limit;" . "\r\n";
	$output .= "\t" . "public \$order_by;" . "\r\n";
	$output .= "\t" . "public \$record_count;" . "\r\n";
	$output .= "\t" . "public \$join;" . "\r\n";
	
	//add the constructor method
	$output .= "\r\n\t" . "function __construct()" . "\r\n";
	$output .= "\t" . "{" . "\r\n";
	$output .= "\t\t" . "parent::__construct();" . "\r\n";
	$output .= "\t\t" . "\$this->table = '" . $table . "';" . "\r\n";
	$output .= "\t" . "}" . "\r\n\r\n";
	
	//add the get() method
	$output .= "\t" . "function get()" . "\r\n";
	$output .= "\t" . "{" . "\r\n";
		//loop through the field_names again and build the logic
		foreach($field_names as $p)
		{
			$output .= "\t\t" . "(\$this->".$p.") ? \$this->db->where('".$p."', \$this->".$p.") : NULL ;" . "\r\n";
		}
		//limits and order_by
		$output .= "\r\n\t\t" . "(\$this->limit AND !\$this->offset) ? \$this->db->limit(\$this->limit) : NULL ;" . "\r\n";
		$output .= "\t\t" . "(\$this->limit AND \$this->offset) ? \$this->db->limit(\$this->limit, \$this->offset) : NULL ;" . "\r\n";
		$output .= "\t\t" . "(\$this->order_by) ? \$this->db->order_by(\$this->order_by) : NULL ;" . "\r\n\r\n";
		
		//the join 
		$output .= "\t\t" . "if(is_array(\$this->join))" . "\r\n";
		$output .= "\t\t" . "{" . "\r\n";
		$output .= "\t\t\t" . "foreach(\$this->join as \$join_table => \$fields)" . "\r\n";
		$output .= "\t\t\t" . "{" . "\r\n";
		$output .= "\t\t\t\t" . "\$join_type = (array_key_exists(2, \$fields)) ? \$fields[2] : 'LEFT' ;" . "\r\n";
		$output .= "\t\t\t\t" . 
					"\$this->db->join(\$join_table, \$this->table.'.'.\$fields[0] .'='.\$join_table.'.'.\$fields[1], \$join_type);" . "\r\n";
		$output .= "\t\t\t" . "}" . "\r\n";
		$output .= "\t\t" . "}" . "\r\n";
		
		//the query statement
		$output .= "\r\n\t\t" . "\$query = \$this->db->get(\$this->table);" . "\r\n\r\n";
		//assign record count to property
		$output .= "\t\t" . "\$this->record_count = \$query->num_rows();" . "\r\n\r\n";
		//check the query result
		$output .= "\t\t" . "if(\$query->num_rows() > 0)" . "\r\n";
		$output .= "\t\t" . "{" . "\r\n";
		$output .= "\t\t\t" . "if(\$this->".$primary_key.")" . "\r\n";
		$output .= "\t\t\t" . "{" . "\r\n";
		$output .= "\t\t\t\t" . "return \$query->row();" . "\r\n";
		$output .= "\t\t\t" . "}" . "\r\n";
		$output .= "\t\t\t" . "else" . "\r\n";
		$output .= "\t\t\t" . "{" . "\r\n";
		$output .= "\t\t\t\t" . "return \$query->result();" . "\r\n";
		$output .= "\t\t\t" . "}" . "\r\n";
		$output .= "\t\t" . "}" . "\r\n";
		$output .= "\t\t" . "else" . "\r\n";
		$output .= "\t\t" . "{" . "\r\n";
		$output .= "\t\t\t" . "return FALSE;" . "\r\n";
		$output .= "\t\t" . "}" . "\r\n";
	//EOM get()
		$output .= "\t" . "}" . "\r\n\r\n";
		
	//add the save() method
		$output .= "\t" . "function save()" . "\r\n";
		$output .= "\t" . "{" . "\r\n";
		$output .= "\t\t" . "\$data = array();" . "\r\n";
		//loop through field names and build property logic
		foreach($field_names as $p)
		{
			$output .= "\t\t" . "(\$this->".$p.") ? \$data['".$p."'] = \$this->".$p." : NULL ;" . "\r\n";
		}
		$output .= "\r\n\t\t" . "(\$this->".$primary_key.") ? \$this->db->where('".$primary_key."', \$this->".$primary_key.")->update(\$this->table, \$data) : NULL ;" . "\r\n";
		$output .= "\t\t" . "(!\$this->".$primary_key.") ? \$this->db->where('".$primary_key."', \$this->".$primary_key.")->insert(\$this->table, \$data) : NULL ;" . "\r\n";
		$output .= "\t\t" . "return (\$this->db->affected_rows() > 0) ? TRUE : FALSE ; " . "\r\n";
		$output .= "\t" . "}" . "\r\n\r\n";
	//EOM save()
	
	//add the delete() method
		$output .= "\t" . "function delete()" . "\r\n";
		$output .= "\t" . "{" . "\r\n";
		$output .= "\t\t" . "(\$this->".$primary_key.") ? \$this->db->where('".$primary_key."', \$this->".$primary_key.")->delete(\$this->table) : NULL ;" . "\r\n";
		$output .= "\t\t" . "return (\$this->db->affected_rows() > 0) ? TRUE : FALSE ; " . "\r\n";
		$output .= "\t" . "}" . "\r\n\r\n";
	//EOM delete()
	
	//Add the count_recs method
		$output .= "\t" . "function count_recs()" . "\r\n";
		$output .= "\t" . "{" . "\r\n";
		$output .= "\t\t" . "return (\$this->get()) ? count(\$this->get()) : 0 ; " . "\r\n";
		$output .= "\t" . "}" . "\r\n";
	//EOM count_recs()
	
	//close the class
	$output .= "}" . "\r\n\r\n";
	$output .= "/* End of file ".$modelname.".php */" . "\r\n";
	$output .= "/* Location: ./system/application/models/".$modelname.".php */";
	
	
	//create the model file
	$success = FALSE;
	$filename = $modelname . ".php";
	$handle = fopen($db.'/models/'.$filename, 'w') or die('Could not open file.');
	if(fwrite($handle, $output))
	{
		$success = TRUE;
	}
	fclose($handle);
	
	if($success)
	{
		echo '<p class="message">Your model: ' . $modelname . '.php has been created in ' . $db . '/models.</p>';
		echo '<a href="'.$_SERVER['PHP_SELF'].'">Create Another</a>';
	}
	
?>

<?php else: ?>

<form action="" method="post" name="ci_crud">
	<h5>Database Details</h5>
	<p><label>Host</label><input type="text" name="hostname" value="localhost" /></p>
    <p><label>Username</label><input type="text" name="username" value="root" /></p>
    <p><label>Password</label><input type="text" name="password" value="" /></p>
    <p><label>Database</label><input type="text" name="database" value="" /></p>
    <p><label>Table</label><input type="text" name="tablename" value="" /></p>
    <h5>Model Details</h5>
    <p><label>Model Name</label><input type="text" name="modelname" value="" /></p>
    <p><input type="submit" name="submit" value="Create Model" /></p>
</form>
<?php endif; ?>
</div>
<div id="footer"><p>copyright 2009 http://clip.wired4tech.com coming soon.</p></div>
</body>
</html>
